package com.zxl.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.mapper.SysGoodsTypeMapper;
import com.zxl.common.model.bo.goods.GoodsTypeListBo;
import com.zxl.common.model.bo.goods.GoodsTypeSaveBo;
import com.zxl.common.model.pojo.SysGoodsType;
import com.zxl.common.service.SysGoodsTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxw
 * @since 2023-03-30
 */
@Service
public class SysGoodsTypeServiceImpl extends ServiceImpl<SysGoodsTypeMapper, SysGoodsType> implements SysGoodsTypeService {

    @Resource
    private SysGoodsTypeMapper sysGoodsTypeMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(GoodsTypeSaveBo bo) {
        SysGoodsType entity = new SysGoodsType();
        BeanUtil.copyProperties(bo, entity);
        entity.setCreateTime(new Date());
        entity.setCreateId(bo.getUserId());
        save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(GoodsTypeSaveBo bo) {
        if(bo.getId() == null) {
            throw new BusinessException("请传入分类id");
        }
        SysGoodsType entity = new SysGoodsType();
        BeanUtil.copyProperties(bo, entity);
        entity.setId(bo.getId());
        entity.setUpdateTime(new Date());
        entity.setUpdateId(bo.getUserId());
        updateById(entity);
    }

    @Override
    public List<SysGoodsType> getList(GoodsTypeListBo bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        List<SysGoodsType> list = sysGoodsTypeMapper.getList(bo);
        return list;
    }

}
